<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="玩家ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入玩家ID" clearable  />
      </el-form-item>

      <el-form-item label="订单ID" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入订单ID" clearable  />
      </el-form-item>
      <el-form-item label="Id" prop="goodsId">
        <el-input v-model="queryParams.goodsId" placeholder="请输入盲盒id   /  出售的订单ID /积分商品id  /积分盒子id" clearable
           />
      </el-form-item>
      <el-form-item label="第几套商品" prop="num">
        <el-input v-model="queryParams.num" placeholder="请输入第几套商品 / -1代表积分兑换 或 抽奖" clearable
           />
      </el-form-item>

      <el-form-item label="下单时间" prop="addtime">
        <el-input v-model="queryParams.addtime" placeholder="请输入下单时间" clearable  />
      </el-form-item>
      <el-form-item label="付款时间" prop="payTime">
        <el-input v-model="queryParams.payTime" placeholder="请输入付款时间" clearable  />
      </el-form-item>

      <el-form-item label="是否发放FIRST赏" prop="isFirst">
        <el-select v-model="queryParams.isFirst" clearable placeholder="请选择是否发放FIRST赏">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否发放LAST赏" prop="isLast">
        <el-select v-model="queryParams.isLast" placeholder="请选择是否发放LAST赏">
          <el-option v-for="item in [
            { value: 1, label: '发' },
            { value: 2, label: '否' },
            { value: 3, label: '一番赏' },
          ]" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="中奖情况" prop="isJiang">
        <el-select v-model="queryParams.isJiang" placeholder="请选择中奖情况">
          <el-option v-for="item in [
            { value: 0, label: '未中奖' },
            { value: 1, label: 'FIRST赏' },
            { value: 2, label: 'LAST赏' },
          ]" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="订单类型" prop="type">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型">
          <el-option v-for="item in dict.type.order_type" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="完成时间" prop="overTime">
        <el-input v-model="queryParams.overTime" placeholder="请输入完成时间" clearable  />
      </el-form-item>
      <el-form-item label="优惠券id" prop="couponId">
        <el-input v-model="queryParams.couponId" placeholder="请输入优惠券id" clearable  />
      </el-form-item>
      <el-form-item label="优惠券金额" prop="couponMoney">
        <el-input v-model="queryParams.couponMoney" placeholder="请输入优惠券金额" clearable
           />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:order:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:order:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange"
      @sort-change="dataListSortChangeHandle">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="订单ID" align="center" prop="id" />

      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户" align="center" prop="username" width="200">
        <template slot-scope="scope">
          <div style="display: flex; justify-content: center; align-items: center">
            <el-avatar :src="scope.row.headimg"></el-avatar>
            <div>{{ scope.row.username }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="orderNum" />
      <el-table-column label="盒子id" sortable="custom" align="center" prop="goodsId" />
      <el-table-column label="第几套商品" sortable="custom" align="center" prop="num">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.num == -1">无限赏</el-tag>
          <span v-else>{{ scope.row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单总金额" sortable="custom" align="center" prop="total" />
      <el-table-column label="实际支付金额" sortable="custom" align="center" prop="totalPrice" />
      <el-table-column label="盲盒金额" sortable="custom" align="center" prop="price" />
      <el-table-column label="订单状态" align="center" prop="status" width="120" sortable="custom">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.status == 1">未支付</el-tag>
          <el-tag type="success" v-else-if="scope.row.status == 2">已支付并开箱</el-tag>
          <el-tag type="primary" v-else-if="scope.row.status == 3">已完成</el-tag>
          <el-tag v-else-if="scope.row.status == 4">取消</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="下单时间"
        sortable="custom"
        align="center"
        prop="addtime"
      >
        <template slot-scope="scope">
          <span>{{
          scope.row.createDate
          }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="付款时间" sortable="custom" align="center" prop="payTime">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.payTime, "{y}-{m}-{d} {hh}:{mm}:{ss}")
          }}</span>
        </template>
      </el-table-column> -->

      <!-- <el-table-column label="" align="center" prop="payType" /> -->
      <el-table-column label="抽数类型" sortable="custom" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.totalPrice / scope.row.price == 1">一发</el-tag>
          <el-tag type="success" v-else-if="scope.row.totalPrice / scope.row.price == 3">三连发</el-tag>
          <el-tag type="success" v-else-if="scope.row.totalPrice / scope.row.price == 5">五连发</el-tag>
          <el-tag type="primary" v-else-if="scope.row.totalPrice / scope.row.price == 10">十连发</el-tag>
          <el-tag v-else>全要</el-tag>
          <!-- <dict-tag type="success" :options="dict.type.cou_num" :value="scope.row.type" /> -->
        </template>
      </el-table-column>
      <el-table-column label="使用余额金额" sortable="custom" align="center" prop="money" />
      <!-- <el-table-column label="使用次元币金额" sortable="custom" align="center" prop="integral" />
      <el-table-column label="是否发放FIRST赏" sortable="custom" align="center" prop="isFirst">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.isFirst == 1">是</el-tag>
          <el-tag type="success" v-else-if="scope.row.isFirst == 2">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否LAST发放赏" sortable="custom" align="center" prop="isLast">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.isLast == 1">发</el-tag>
          <el-tag type="success" v-else-if="scope.row.isLast == 2">否</el-tag>
          <el-tag type="primary" v-else-if="scope.row.isLast == 3"
            >一番赏</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="中奖状态" sortable="custom" align="center" prop="isJiang">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isJiang == 0">未中奖</el-tag>
          <el-tag type="info" v-else-if="scope.row.isJiang == 1"
            >FIRST赏</el-tag
          >
          <el-tag type="primary" v-else-if="scope.row.isJiang == 2"
            >LAST赏</el-tag
          >
        </template>
      </el-table-column> -->
      <el-table-column label="完成时间" sortable="custom" align="center" prop="createDate">
        <template slot-scope="scope">
          <span>{{
            scope.row.createDate
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="订单类型"
        sortable="custom"
        align="center"
        prop="orderType"
      >
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.orderType == 1
                ? 'success'
                : scope.row.orderType == 2
                ? 'warning'
                : scope.row.orderType == 3
                ? 'info'
                : scope.row.orderType == 4
                ? 'danger'
                : 'primary'
            "
            >{{
              scope.row.orderType == 1
                ? "下单流程"
                : scope.row.orderType == 2
                ? "集市获得"
                : scope.row.orderType == 3
                ? "积分兑换"
                : scope.row.orderType == 4
                ? "积分抽奖"
                : "未知"
            }}</el-tag
          >
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="优惠券id" align="center" prop="couponId" /> -->
      <el-table-column label="反馈信息" align="center" prop="message" />
      <!-- <el-table-column label="${comment}" align="center" prop="wxIs" />
      <el-table-column label="${comment}" align="center" prop="creator" />
      <el-table-column label="${comment}" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="盲盒id   /  出售的订单ID /积分商品id  /积分盒子id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入盲盒id   /  出售的订单ID /积分商品id  /积分盒子id" />
        </el-form-item>
        <el-form-item label="第几套商品 / -1代表积分兑换 或 抽奖" prop="num">
          <el-input v-model="form.num" placeholder="请输入第几套商品 / -1代表积分兑换 或 抽奖" />
        </el-form-item>
        <el-form-item label="订单总金额" prop="total">
          <el-input v-model="form.total" placeholder="请输入订单总金额" />
        </el-form-item>
        <el-form-item label="实际支付金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入实际支付金额" />
        </el-form-item>
        <el-form-item label="盲盒金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入盲盒金额" />
        </el-form-item>
        <el-form-item label="下单时间" prop="addtime">
          <el-input v-model="form.addtime" placeholder="请输入下单时间" />
        </el-form-item>
        <el-form-item label="付款时间" prop="payTime">
          <el-input v-model="form.payTime" placeholder="请输入付款时间" />
        </el-form-item>
        <el-form-item label="备注" prop="message">
          <el-input v-model="form.message" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="使用余额金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入使用余额金额" />
        </el-form-item>
        <el-form-item label="使用次元币金额" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入使用次元币金额" />
        </el-form-item>
        <el-form-item label="是否发放FIRST赏1发 2 否" prop="isFirst">
          <el-input v-model="form.isFirst" placeholder="请输入是否发放FIRST赏1发 2 否" />
        </el-form-item>
        <el-form-item label="是否发放LAST赏 1 发2 否 3一番赏" prop="isLast">
          <el-input v-model="form.isLast" placeholder="请输入是否发放LAST赏 1 发2 否 3一番赏" />
        </el-form-item>
        <el-form-item label="0未中奖 1 FIRST赏 2 LAST赏" prop="isJiang">
          <el-input v-model="form.isJiang" placeholder="请输入0未中奖 1 FIRST赏 2 LAST赏" />
        </el-form-item>
        <el-form-item label="完成时间" prop="overTime">
          <el-input v-model="form.overTime" placeholder="请输入完成时间" />
        </el-form-item>
        <el-form-item label="优惠券id" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠券id" />
        </el-form-item>
        <el-form-item label="优惠券金额" prop="couponMoney">
          <el-input v-model="form.couponMoney" placeholder="请输入优惠券金额" />
        </el-form-item>
        <el-form-item label="${comment}" prop="wxIs">
          <el-input v-model="form.wxIs" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="createDate">
          <el-date-picker clearable v-model="form.createDate" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
} from "@/api/order/list.js";
import { getMy_user } from "@/api/users/user.js";
export default {
  name: "Order",
  dicts: ["cou_num", "order_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        isAsc: "desc", // 排序，asc／desc
        orderByColumn: "createDate", // 排序，字段
        pageNum: 1,
        pageSize: 10,
        userId: null,
        orderNum: null,
        goodsId: null,
        num: null,
        total: null,
        totalPrice: null,
        price: null,
        status: null,
        addtime: null,
        payTime: null,
        message: null,
        payType: null,
        type: null,
        money: null,
        integral: null,
        isFirst: null,
        isLast: null,
        isJiang: null,
        overTime: null,
        orderType: null,
        couponId: null,
        couponMoney: null,
        wxIs: null,
        creator: null,
        createDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "$comment不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "订单号不能为空", trigger: "blur" },
        ],
        total: [
          { required: true, message: "订单总金额不能为空", trigger: "blur" },
        ],
        status: [
          {
            required: true,
            message: "1 未支付  2 已支付并开箱 3 已完成 4 取消不能为空",
            trigger: "blur",
          },
        ],
        addtime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" },
        ],
        payType: [
          {
            required: true,
            message: "1微信 2 云闪付 不能为空",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "订单类型 1 一发 2 五连发 3 十连发 4 全要不能为空",
            trigger: "change",
          },
        ],
        couponId: [
          { required: true, message: "优惠券id不能为空", trigger: "blur" },
        ],
        couponMoney: [
          { required: true, message: "优惠券金额不能为空", trigger: "blur" },
        ],
        wxIs: [
          { required: true, message: "$comment不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.queryUser(this.orderList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        orderNum: null,
        goodsId: null,
        num: null,
        total: null,
        totalPrice: null,
        price: null,
        status: 0,
        addtime: null,
        payTime: null,
        message: null,
        payType: null,
        type: null,
        money: null,
        integral: null,
        isFirst: null,
        isLast: null,
        isJiang: null,
        overTime: null,
        orderType: null,
        couponId: null,
        couponMoney: null,
        wxIs: null,
        creator: null,
        createDate: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrder(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
    //查询用户信息
    async queryUser(arr) {
      for (let index = 0; index < arr.length; index++) {
        console.log(arr[index].userId);
        await getMy_user(arr[index].userId).then((response) => {
          if (response.data) {
            this.$set(
              this.orderList[index],
              "username",
              response.data.nickname
            );
            this.$set(this.orderList[index], "headimg", response.data.headimg);
          }
        });
      }
    },
    //排序
    dataListSortChangeHandle(data) {
      if (!data.order || !data.prop) {
        this.queryParams.orderByColumn = "";
        this.queryParams.isAsc = "";
        return false;
      }
      this.queryParams.isAsc = data.order.replace(/ending$/, "");
      this.queryParams.orderByColumn = data.prop
        .replace(/([A-Z])/g, "_$1")
        .toLowerCase();
      this.getList();
    },
  },
};
</script>
